<?php

namespace App\Sidong\Controllers;

use App\Models\Coach;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\Str;

class CoachController extends AdminController
{
    protected $title = '教练管理';

    public function index(Content $content)
    {
        $tab = new Tab();

        $tab->add('全部教练', $this->grid()->render());
        $tab->add('私教', $this->grid(1)->render());
        $tab->add('团课教练', $this->grid(2)->render());

        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '教练列表'])
//            ->body($this->grid());
            ->body($tab->render());
    }

    public function create(Content $content)
    {
        return $content
            ->title('新增教练')
            ->breadcrumb(['text' => '教练列表', 'url' => 'coaches'], ['text' => '新增教练'])
            ->body($this->form());
    }

    public function edit($id, Content $content)
    {
        return $content
            ->title('编辑教练')
            ->breadcrumb(['text' => '教练列表', 'url' => 'coaches'], ['text' => '编辑教练'])
            ->body($this->form()->edit($id));
    }

    protected function grid($type = 0)
    {
        $grid = new Grid(new Coach);
        $grid->disableBatchActions();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });
        $grid->model()
            ->orderByDesc('sort')
            ->orderByDesc('created_at')
            ->orderByDesc('is_enable');
        if ($type) {
            $grid->model()->where('type', $type);
        }
        $grid->column('id', __('ID'))->sortable();
        $grid->column('realname', '姓名');
        $grid->column('phone', '手机号码');
        $grid->column('cover_url', '封面图')->lightbox(['width' => 35, 'height' => 35]);
        $grid->column('type', '类型')->display(function($gender) {
            return $gender == 1 ? '<span class="label label-warning">私教</span>' : '<span class="label label-info">团课教练</span>';
        });
        $grid->column('gender', '性别')->display(function($gender) {
            return $gender == 1 ? '<span class="label label-success">男</span>' : '<span class="label label-danger">女</span>';
        });
        $grid->column('price', '价格/节');
        $grid->column('sale_num', '起售节数');
        $grid->column('brief_intro', '一句话简介')->display(function($brief_intro) {
            return Str::limit($brief_intro, '28', '...');
        });
        $grid->column('expert', '擅长项目')->display(function($expert) {
            return implode(',', $expert);
        });
        $grid->column('qualification', '专业资质')->display(function($qualification) {
            return Str::limit($qualification, '50', '...');
        });
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
        ];
        $grid->column('is_enable', '启用状态')->switch($states);
        return $grid;
    }

    protected function form()
    {
        $admin = \Admin::user();
        $form = new Form(new Coach);
        $form->hidden('tenant_id')->default($admin->tenant_id);
        $form->text('realname', '姓名')
            ->placeholder('请输入教练真实姓名')
            ->rules('required');
        $form->mobile('phone', '手机号码')
            ->options(['mask' => '999 9999 9999'])
            ->placeholder('请输入教练手机号码')
            ->rules('required|phone:CN,mobile')
            ->width(100);
        $form->radio('type', '类型')->options([1 => '私教', 2=> '团课教练'])->default(1);
        $form->radio('gender', '性别')->options([1 => '男', 2=> '女'])->default(1);
        $form->image('cover_url', '封面图')->help('显示在小程序教练列表的封面图片，建议尺寸800*800')->rules('required')->crop(800, 800);
        $form->currency('price', '价格')
            ->placeholder('请输入单节私教课价格')
            ->help('单节私教课的价格，如需输入小数请在数字后继续输入点号即可')
            ->width('200px')
            ->symbol('￥')
            ->rules('required');
        $form->number('sale_num', '起售节数')
            ->placeholder('请输入私教课起售节数')
            ->min(1)
            ->default(1)
            ->rules('required')
            ->help('用户最低购买私教课的节数，即：xx节起售');
        $form->text('brief_intro', '格言')
            ->placeholder('请输入教练格言，6-12个字符内。如：健身使我快乐！')->rules('required');
        $form->multipleSelect('expert', '擅长项目')->options(Coach::getTagsSelectOptions());
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $form->switch('is_enable', '是否启用')
            ->default(1)
            ->help('注意：如果选择禁用则教练不会出现在小程序上的教练列表中。')
            ->states($states);
        $form->number('sort', '排序')
            ->default(1)
            ->min(1)
            ->help('输入排序数字，值越大越靠前');
        $form->textarea('qualification', '专业资质')
            ->help('请输入教练专业资质。如果有多个请一行一个')
            ->rules('required');
        $form->saving(function($form) {
            $form->phone = str_replace(' ', '', $form->phone);
            if(is_array($form->expert) && !empty($form->expert)) {
                $form->expert = array_filter($form->expert);
            }
        });
        return $form;
    }

}
